<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
    <title>客户历史订单查询</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.8.6/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.8.6/themes/icon.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.8.6/jquery.edatagrid.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
</head>
<body style="margin: 15px">

<div id="p" class="easyui-panel" title="客户基本信息" style="width: 700px;height: 100px;padding: 10px">
    <table cellspacing="8px">
        <tr>
            <td>客户编号：</td>
            <td><input class="easyui-textbox" class="easyui-textbox" id="khno" name="khno" readonly="true"/></td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td>客户名称</td>
            <td><input class="easyui-textbox" id="name" name="name" readonly="true"/></td>
        </tr>
    </table>
</div>

<br/>
<table id="dg" title="客户历史订单" class="easyui-datagrid" fitColumns="true" pagination="true" rownumbers="true"
       url="${pageContext.request.contextPath}/order/list.do?cusId=${param.cusId}" style="width: 900px;height: 400px">
    <thead>
    <tr>
        <th field="id" width="50" align="center" hidden="true">编号</th>
        <th field="orderNo" width="100" align="center">订单号</th>
        <th field="orderDate" width="100" align="center">订购日期</th>
        <th field="address" width="200" align="center">送货地址</th>
        <th field="state" width="50" align="center" formatter="formatState">状态</th>
        <th field="a" width="70" align="center" formatter="formatAction">操作</th>
    </tr>
    </thead>
</table>

<div id="dlg" title="客户历史订单详情" class="easyui-dialog" style="width:700px;height:550px;padding: 10px 20px" closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <table cellspacing="8px">
            <tr>
                <td>订单号：</td>
                <td><input class="easyui-textbox" id="orderNo" name="orderNo" readonly="true"/></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>订购日期</td>
                <td><input class="easyui-textbox" id="orderDate" name="orderDate" readonly="true"/></td>
            </tr>
            <tr>
                <td>送货地址：</td>
                <td><input class="easyui-textbox" id="address" name="address" readonly="true"/></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>总金额</td>
                <td><input class="easyui-textbox" id="totalMoney" name="totalMoney" readonly="true"/></td>
            </tr>
            <tr>
                <td>状态：</td>
                <td><input class="easyui-textbox" id="state" name="state" readonly="true"/></td>
                <td colspan="3">&nbsp;&nbsp;&nbsp;&nbsp;</td>
            </tr>
        </table>
        <br/>
        <table id="dg2" title="订购详情" class="easyui-datagrid" fitColumns="true" pagination="true" rownumbers="true" style="width: 640px;height: 280px">
            <thead>
            <tr>
                <th field="id" width="50" align="center" hidden="true">编号</th>
                <th field="goodsName" width="150" align="center">商品名称</th>
                <th field="goodsNum" width="50" align="center">订购数量</th>
                <th field="unit" width="50" align="center">单位</th>
                <th field="price" width="50" align="center">单价(元)</th>
                <th field="sum" width="80" align="center">金额(元)</th>
            </tr>
            </thead>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:closeOrderDetailsDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>

</body>
</html>

<script type="text/javascript">
    $(function () {

        $.post("${pageContext.request.contextPath}/customer/findById.do", {id: '${param.cusId}'}, function (result) {
            $("#khno").textbox("setValue", result.khno);
            $("#name").textbox("setValue", result.name);
        }, "json");

    });

    function formatState(val, row, index) {
        if (val == 1) {
            return "已回款";
        } else {
            return "未回款";
        }
    }

    function formatAction(val, row, index) {
        return "<a href='javascript:openOrderDetailsDialog(" + row.id + ")'>查看订单明细</a>"
    }

    function openOrderDetailsDialog(orderId) {
        $.post("${pageContext.request.contextPath}/order/findById.do", {id: orderId}, function (result) {
            $("#fm").form('load', result);
            if (result.state == 0) {
                $("#state").textbox("setValue", "未回款");
            } else if (result.state == 1) {
                $("#state").textbox("setValue", "已回款");
            }
        }, "json");

        $.post("${pageContext.request.contextPath}/orderDetails/getTotalPrice.do", {orderId: orderId}, function (result) {
            $("#totalMoney").textbox("setValue", result.totalMoney);
        }, "json");

        $("#dg2").datagrid({url: "${pageContext.request.contextPath}/orderDetails/list.do",queryParams:{orderId:orderId}}).datagrid('load', {
            "orderId": orderId
        });

        $("#dlg").dialog("open").dialog("setTitle", "订单详情");
    }

    function closeOrderDetailsDialog(){
        $("#dlg").dialog("close");
    }

</script>
